package us.lithub.ui;

import us.lithub.R;
import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.fedorvlasov.lazylist.ImageLoader;

/**
 * The Class RowBook is a view to display a book in a list view
 */
public class RowBook extends RelativeLayout {
	
	/** The m context. */
	private Context mContext;
	
	/** The m isbn. */
	private String mIsbn;
	
	/**
	 * Instantiates a new row book.
	 *
	 * @param context the context
	 * @param attrs the attrs
	 */
	public RowBook(Context context, AttributeSet attrs) {
		super(context, attrs);
		this.mContext = context;
	}
	
	/**
	 * Sets the author.
	 *
	 * @param author the new author
	 */
	public void setAuthor(String author) {
		TextView tv = (TextView) findViewById(R.id.lblAuthor);
		tv.setText(author);
	}
	
	/**
	 * Sets the title.
	 *
	 * @param title the new title
	 */
	public void setTitle(String title) {
		TextView tv = (TextView) findViewById(R.id.lblTitle);
		tv.setText(title);
	}
	
	/**
	 * Sets the image.
	 *
	 * @param isbn the new image
	 */
	public void setImage(String isbn) {
		ImageView iv = (ImageView) findViewById(R.id.imgIcon);
		new ImageLoader(mContext).DisplayImage("http://covers.openlibrary.org/b/isbn/" + isbn + "-M.jpg", iv);
	}
	
	/**
	 * Sets the isbn.
	 *
	 * @param isbn the new isbn
	 */
	public void setIsbn(String isbn) {
		this.mIsbn = isbn;
	}

	/**
	 * Gets the isbn.
	 *
	 * @return the isbn
	 */
	public String getIsbn() {
		return mIsbn;
	}
	
	/**
	 * Sets the request string.
	 *
	 * @param statusCode the status code
	 * @param ownerID the owner id
	 * @param username the username
	 * @param ownername the ownername
	 * @param myUserID the my user id
	 */
	public void setRequestString(int statusCode, int ownerID, String username, String ownername, int myUserID) {
		TextView tv = (TextView) findViewById(R.id.lblRequest);
		
		if (statusCode == 102 && ownerID != myUserID) {
			tv.setText("Return this book to " + ownername);
		} else if (statusCode == 102) {
			tv.setText("Request this book back from " + username);
		} else {
			tv.setText("Lend this book");
			tv.setTextColor(Color.GREEN);
		}
	}
}